import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/helper.dart';
import 'package:yp/services/api.dart';

import 'state.dart';

class DrawBillController extends GetxController {
  bool _isNeedDill = false;
  bool get isNeedDill => _isNeedDill;
  final state = DrawBillState();

  @override
  void onInit() {
    super.onInit();

    // 获取用户信息
    // var params = {
    //   'access_token': SpUtil.getString('token'),
    //   'mobile': SpUtil.getString('number'),
    // };
    // Api.searchMember(params).listen((event) {
    //   if (event['code'] == 0) {
    //     state.userInformationData = event;
    //   } else {
    //     state.userInformationData = null;
    //   }
    //   update();
    // });

    Api.noBill({"platform_id": 3}).listen((event) {
      if (event['code'] == 0) {}
    });

    if (Get.arguments["isNeedBill"] != null) {
      state.isNeedDrawBill = Get.arguments["isNeedBill"];
      // 'consumer_tax_account': state.controller2.text,
      //   'consumer_bank_name': state.controller5.text,
      //   'consumer_bank_address': state.controller3.text,
      //   'consumer_addr': state.controller3.text,
      //   'consumer_bank_account': state.controller6.text,
      //   'consumer_bank_telephone': state.controller4.text,
      //   'consumer_telephone': state.controller4.text,

      if (Get.arguments["p"] != null) {
        var p = Get.arguments["p"];
        state.controller2.text = p["consumer_tax_account"];
        state.controller5.text = p["consumer_bank_name"];
        state.controller3.text = p["consumer_bank_address"];
        state.controller6.text = p["consumer_bank_account"];
        state.controller4.text = p["consumer_tax_account"];
        state.eMailAddress.text = p["taker_email"];
        state.dillHeader.text = p["invoice_title"];

        state.isPersonalInvoice = p["invoice_type"] == 1;
        if (p["invoice_type"] == 1) {
          state.billType = '(个人/非企业)';
        } else {
          state.billType = '(企业)';
        }
      }

      print("Get.arguments=${Get.arguments}");
      // if (Get.arguments["billHeader"] == "企业") {
      //   state.isPersonalInvoice = false;
      // } else {
      //   state.isPersonalInvoice = true;
      // }
      // state.dillHeader.text = Get.arguments["billHeader"] ?? '';
      // update();
    }
    if (Get.arguments["tml_num_id"].toString().isNotEmpty) {
      Api.queryInvoiceDetails({"tml_num_id": Get.arguments["tml_num_id"]})
          .listen((event) {
        if (event['code'] == 0) {
          if (event["invoice_info"] != null) {
            state.isNeedDrawBill = true;
            var info = event["invoice_info"];
            state.isPersonalInvoice = info["invoice_type"] == 1;
            state.dillHeader.text = info["invoice_title"];
            state.controller2.text = info["consumer_tax_account"];
            state.controller3.text = info["consumer_bank_address"];
            state.controller4.text = info["consumer_telephone"];
            state.controller5.text = info["consumer_bank_name"];
            state.controller6.text = info["consumer_bank_account"];
            state.eMailAddress.text = info["taker_email"];
            update();
          }
        }
      });
    }

    var params = {
      'use_platform': 2,
      'access_token': SpUtil.getString('token'),
      'usr_num_id': SpUtil.getInt('empeNumId'),
    };
    Api.commonInvoiceHeader(params).listen((event) {
      if (event['code'] == 0) {
        List list = event['invoice_title_infos'];
        if (list.length > 0) {
          list.forEach((element) {
            if (element['default_sign'] == 1) {
              print('+++++++++**************  成功  常用发票抬头  event = $element');

              state.isPersonalInvoice = element['invoice_type'] == 1;
              state.dillHeader.text = element['invoice_title'];
              state.controller2.text = element['consumer_tax_account'];
              element['invoice_type'] == 1
                  ? state.billType = '(个人/非企业)'
                  : state.billType = '(企业)';
              state.controller3.text = element['consumer_addr'];
              state.controller4.text = element['consumer_telephone'];
              state.controller5.text = element['consumer_bank_name'];
              state.controller6.text = element['consumer_bank_account'];
            }
          });
        }
      }

      update();
    }, onError: (error) {
      print('+++++++++**************  失败  常用发票抬头  error = $error');
    });
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  /// ignore: slash_for_doc_comments
  /**
  * David
  * 是否需要发票选择框
  */
  void changeDrawBillSelect(BuildContext context, bool isNeed) {
    if (!isNeed) {
      state.billType = '不需要';
      state.dillHeader.text = '';
    } else {
      state.billType = '(企业)';
    }
    state.isNeedDrawBill = !state.isNeedDrawBill;
    update();
  }

  // ignore: slash_for_doc_comments
  /**
  * David
  * 发票抬头
  */
  void changeInvoiceHeade(BuildContext context, bool isPersonal) {
    if (isPersonal) {
      state.billType = '(个人/非企业)';
      // state.billHeader = "个人发票抬头";
    } else {
      state.billType = '(企业)';
      // state.billHeader = "企业发票抬头";
    }
    state.isPersonalInvoice = !state.isPersonalInvoice;
    update();
  }

  // ignore: slash_for_doc_comments
  /**
  * David
  * 商品明细
  */
  void ChangeCommodityDetail(BuildContext context) {
    state.isNeedCommodityDetail = !state.isNeedCommodityDetail;
    update();
  }

  // ignore: slash_for_doc_comments
  /**
  * David
  * 提交
  */
  void submit(BuildContext context) {
    if (state.isNeedDrawBill) {
      if (state.dillHeader.text == '' || state.dillHeader.text == null) {
        toast('发票抬头不能为空');
        return;
      }
      if (state.eMailAddress.text.isEmpty) {
        toast('邮箱不能为空');
        return;
      }
      if (!state.eMailAddress.text.isEmail) {
        toast('邮箱格式错误');
        return;
      }
    }

    // 'invoice_sign': this.needInvoice=='NoNeed'?0:1,
    // 'invoice_medium': 1,
    // 'invoice_type': this.invoiceType,
    // 'invoice_title': this.invoiceTitle,
    // 'consumer_invoice_content': this.invoiceContent,
    // 'consumer_tax_account': this.taxNum,
    // 'consumer_bank_name': this.bankName,
    // 'consumer_bank_address': this.addressDetail,
    // 'consumer_bank_account': this.bankAccount,
    // 'consumer_bank_telephone': this.entPhone,
    // 'consumer_telephone': "",
    // 'taker_telephone': this.selectedReceiver.contact_no,
    // 'taker_prv_num_id': this.selectedReceiver.prv_num_id,
    // 'taker_city_num_id': this.selectedReceiver.city_num_id,
    // 'taker_city_area_num_id': this.selectedReceiver.city_area_num_id,
    // 'taker_addr': this.selectedReceiver.adr,
    // 'selectedReceiver':this.selectedReceiver,
    // 'taker_email':this.taker_email,

    var params = {
      'tml_num_id': Get.arguments["tml_num_id"],
      'invoice_model': {
        'invoice_sign': state.isNeedDrawBill ? 1 : 0,
        'invoice_medium': 1,
        'invoice_type': state.isPersonalInvoice ? 1 : 2,
        'invoice_title': state.dillHeader.text,
        'consumer_invoice_content': state.isNeedCommodityDetail ? 1 : 0,
        'consumer_tax_account': state.controller2.text,
        'consumer_bank_name': state.controller5.text,
        'consumer_bank_address': state.controller3.text,
        'consumer_addr': state.controller3.text,
        'consumer_bank_account': state.controller6.text,
        'consumer_bank_telephone': state.controller4.text,
        'consumer_telephone': state.controller4.text,
        // 'taker_telephone': this.selectedReceiver.contact_no,
        // 'taker_prv_num_id': this.selectedReceiver.prv_num_id,
        // 'taker_city_num_id': this.selectedReceiver.city_num_id,
        // 'taker_city_area_num_id': this.selectedReceiver.city_area_num_id,
        // 'taker_addr': this.selectedReceiver.adr,
        // 'selectedReceiver':this.selectedReceiver,
        'taker_email': state.eMailAddress.text,
      }
    };

    print(params);
    // return;
    Api.createInvoice(params).listen((event) {
      if (event['code'] == 0) {
        toast("开票成功");
      }
    });

    Get.back(result: {
      'a': state.billType,
      'b': state.dillHeader.text,
      'c': params["invoice_model"]
    });
  }
}
